//
//  FPActorViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPActorViewController.h"
#import "FPRankView.h"
#import "FPActorViewCell.h"
#import "FPRankTopModel.h"
#import "FPActorModel.h"

@interface FPActorViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)FPRankView *rankView;
@property(nonatomic,strong)NSMutableArray *personArray;
@property(nonatomic,strong)MBProgressHUD *HUD;
@end

@implementation FPActorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 20)];
    statusBarView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:statusBarView];
    
    [self creatTableView];
   
    [self analysisData];
      [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(actorRefreshAction)];
}
- (void)actorRefreshAction
{
    [self analysisData];
}
- (FPRankView *)rankView
{
    if (!_rankView) {
        _rankView = [[FPRankView alloc]init];
    }
    return _rankView;
}
- (void)creatTableView
{ UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.tableView.rowHeight = kImageH(140);
    self.tableView.delegate  = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //添加头部view;
    self.rankView.frame = CGRectMake(0, 0, kScreenWidth, kImageH(90));
    self.rankView.backgroundColor = [UIColor lightGrayColor];
    self.rankView.alpha = 0.5;
    self.tableView.tableHeaderView = self.rankView;
}
//解析数据
- (NSMutableArray *)personArray
{
    if (!_personArray) {
        _personArray = [[NSMutableArray alloc]init];
    }
    return _personArray;
}
- (void)analysisData
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
     [self.view bringSubviewToFront:_HUD];
   
    if (![[FPNetWork shareInstance]rechability])
    {
        self.HUD.labelText = @"请判断网络";
        return;
    }
    self.HUD.labelText = @"正在为你努力加载";
    //创建url对象
    NSURL *url = [NSURL URLWithString:self.actorUrl];
    //创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    //开启异步联接 用队列的形式进行一步请求数据
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (!data)
        {
          self.HUD.labelText = @"无数据请求";
            return ;
        }
        //请求的所有数据已经全部接受完成的时候 ,才会执行Block快里的方法 此时data就是我们请求下来的数据它会自动传回到这个参数上面
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *topListDic = dic[@"topList"];
        FPRankTopModel *topListModel = [FPRankTopModel fpRankTopModelWithDictionary:topListDic];
        self.rankView.titleName.text = topListModel.name;
        self.rankView.introLable.text = topListModel.summary;
        NSDictionary *personArray = dic[@"persons"];
        for (NSDictionary *personDic in personArray) {
            FPActorModel *model = [FPActorModel withDictionary:personDic];
            [self.personArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.HUD hide:YES afterDelay:1.0];
    }];
    
}
#pragma mark===========实现代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.personArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"actorCell";
    [tableView registerClass:[FPActorViewCell class] forCellReuseIdentifier:cellIndentifier];
    FPActorViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];
    FPActorModel *model = self.personArray[indexPath.row];
    [cell cellForModel:model];
    return cell;
}


@end
